package org.zhouhe.modules.report.action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.zhouhe.base.BaseAction;
import org.zhouhe.modules.report.service.ReportService;
import org.zhouhe.page.PageUtil;
import org.zhouhe.util.InitSystem;
import org.zhouhe.vo.Industry;
import org.zhouhe.vo.KeywordRecommended;
import org.zhouhe.vo.Report;
import org.zhouhe.vo.ReportIndustry;
import org.zhouhe.vo.ReportReco;
import org.zhouhe.vo.ReportType;

public class ReportAction extends BaseAction {

	private static final long serialVersionUID = 8987580734193381792L;
	private PageUtil pageUtil = new PageUtil();
	@Autowired
	private ReportService reportService;
	private Report report = new Report();
	private String k;//关键字
	private Integer i;//行业ID
	private Integer t;//类型ID
	private ReportType reportType = new ReportType();
	private Industry paramIndustry = new Industry();
	private KeywordRecommended keywordRecommended = new KeywordRecommended();
	
	private List<ReportType> reportTypeList;
	private List<ReportIndustry> reportIndustries = new ArrayList<ReportIndustry>();
	private List<KeywordRecommended> keywordRecommendedList = new ArrayList<KeywordRecommended>();
	private List<ReportReco> reportRecoList = new ArrayList<ReportReco>();
	private List<Industry> industryList = new ArrayList<Industry>();
	public String list() {
		reportService.getReportList(pageUtil, report, paramIndustry.getIndustryId(), reportType.getReportTypeId());
		setReportTypeList(reportService.getReportTypeList());
		return "list";
	}
	public String saveOrUpdate(){
		if(report.getReportId() != null){
			setReport(reportService.getReportDetail(getReport().getReportId()));
		}else{
			setReport(new Report());
		}
		if(this.pageUtil == null){
			this.pageUtil = new PageUtil();
		}
		if(report.getDisplayTime() == null){
			report.setDisplayTime(new Timestamp(new Date().getTime()));
		}
		if (report.getPublishInstitution() == null || "".equals(report.getPublishInstitution())) {
			report.setPublishInstitution(InitSystem.paramMap.get("websiteName"));
		}
		if (report.getManifestations() == null || "".equals(report.getManifestations())) {
			report.setManifestations(InitSystem.paramMap.get("manifestations"));
		}
		if (report.getOrderTel() == null || "".equals(report.getOrderTel())) {
			report.setOrderTel(InitSystem.paramMap.get("orderTel"));
		}
		if (report.getDeliveryMethod() == null || "".equals(report.getDeliveryMethod())) {
			report.setDeliveryMethod(InitSystem.paramMap.get("deliveryMethod"));
		}
		if (report.getFax() == null || "".equals(report.getFax())) {
			report.setFax(InitSystem.paramMap.get("fax"));
		}
		if (report.getDeliveryTime() == null || "".equals(report.getDeliveryTime())) {
			report.setDeliveryTime(InitSystem.paramMap.get("deliveryTime"));
		}
		if (report.getPublishDate() == null || "".equals(report.getPublishDate())) {
			report.setPublishDate(new Date());
		}
		
		setReportTypeList(reportService.getReportTypeList());
		List<Integer> tmpList = new ArrayList<Integer>();
		for(ReportIndustry indus:report.getReportIndustries()){
			Industry pIndustry = indus.getIndustry().getIndustry();
			if(pIndustry == null)continue;
			Integer pid = pIndustry.getIndustryId();
			if(!tmpList.contains(pid)){
				pIndustry.setOrderId(0);
				for (ReportReco reco : report.getReportRecos()) {
					Integer recoIndId = reco.getIndustry().getIndustryId();
					if (pid.equals(recoIndId)) {
						pIndustry.setOrderId(1);
					}
				}
				industryList.add(pIndustry);
				tmpList.add(pid);
			}
		}
		if(report.getVersion() == null){
			String thisYear = new SimpleDateFormat("yyyy").format(new Date());
			Integer year = new Integer(thisYear);
			report.setVersion(year);
		}
		return "saveOrUpdate";
	}
	public String saveOrUpdateDo(){
		if(report.getFree() == null){
			report.setFree(0);
		}
		report.setReportIndustries(new LinkedHashSet<ReportIndustry>(reportIndustries));
		report.setReportRecos(new LinkedHashSet<ReportReco>(reportRecoList));
		String keywords = report.getKeywords();
		if(keywords != null){
			keywords = keywords.replaceAll("[\r]+", "");//换行 => 
			keywords = keywords.replaceAll("[\n]+", " ");//换行 => 半角空格
			keywords = keywords.replaceAll("[　]+", " ");//全角空格 => 半角空格
			keywords = keywords.replaceAll("[ ]+", ",");//多个连续空格 => 一个英文逗号（分隔符）
			keywords = keywords.replaceAll("[,,]+", ",");
			report.setKeywords(keywords);
		}
		if(getReport().getReportId() == null){//save
			reportService.save(report);
		}else{//update
			reportService.update(report);
		}
		return "saveOrUpdateDo";
	}
	public String delete(){
		reportService.delete(report);
		return "delete";
	}
	public String keywordRecoList(){
		keywordRecommendedList = reportService.getKeyWordsRecommended(null);
		return "keywordRecoList";
	}
	public void keywordRecoSaveOrUpdate(){
		reportService.saveOrUpdateKeywordReco(keywordRecommended);//keyword
		InitSystem.reloadKeywordRecommned();
	}
	public String keywordRecoDelete(){
		reportService.deleteKeyword(keywordRecommended);
		InitSystem.reloadKeywordRecommned();
		return "keywordRecoDelete";
	}
	public void keywordUpdateOrder(){
		reportService.updateKeywordOrder(keywordRecommendedList);//keyword
		InitSystem.reloadKeywordRecommned();
	}
	
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	public PageUtil getPageUtil() {
		return pageUtil;
	}

	public void setReportTypeList(List<ReportType> reportTypeList) {
		this.reportTypeList = reportTypeList;
	}

	public List<ReportType> getReportTypeList() {
		return reportTypeList;
	}

	public void setReport(Report report) {
		this.report = report;
	}

	public Report getReport() {
		return report;
	}

	public void setReportIndustries(List<ReportIndustry> reportIndustries) {
		this.reportIndustries = reportIndustries;
	}

	public List<ReportIndustry> getReportIndustries() {
		return reportIndustries;
	}

	public void setParamIndustry(Industry paramIndustry) {
		this.paramIndustry = paramIndustry;
	}

	public Industry getParamIndustry() {
		return paramIndustry;
	}

	public void setReportType(ReportType reportType) {
		this.reportType = reportType;
	}

	public ReportType getReportType() {
		return reportType;
	}
	public void setK(String k) {
		this.k = k;
	}
	public String getK() {
		return k;
	}
	public void setT(Integer t) {
		this.t = t;
	}
	public Integer getT() {
		return t;
	}
	public void setI(Integer i) {
		this.i = i;
	}
	public Integer getI() {
		return i;
	}
	public void setKeywordRecommendedList(List<KeywordRecommended> keywordRecommendedList) {
		this.keywordRecommendedList = keywordRecommendedList;
	}
	public List<KeywordRecommended> getKeywordRecommendedList() {
		return keywordRecommendedList;
	}
	public void setKeywordRecommended(KeywordRecommended keywordRecommended) {
		this.keywordRecommended = keywordRecommended;
	}
	public KeywordRecommended getKeywordRecommended() {
		return keywordRecommended;
	}
	public void setReportRecoList(List<ReportReco> reportRecoList) {
		this.reportRecoList = reportRecoList;
	}
	public List<ReportReco> getReportRecoList() {
		return reportRecoList;
	}
	public void setIndustryList(List<Industry> industryList) {
		this.industryList = industryList;
	}
	public List<Industry> getIndustryList() {
		return this.industryList;
	}
}
